
package com.example.testutil.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

public class ZipUtil {

    private ZipUtil() {
        //参考链接:http://blog.csdn.net/dabing69221/article/details/17074763
        throw new UnsupportedOperationException("cannot be instantiated");
    }

    @SuppressWarnings("resource")
    public static boolean zipfolder(String srcfile, String zipfile) {
        File mFile = new File(srcfile);
        if (!mFile.exists()) {
            return false;
        }
        FileInputStream mInputStream = null;
        ZipOutputStream mZipOutputStream = null;
        if (mFile.isFile()) {
            File mZipfile = new File(zipfile);
            ZipEntry mEntry = new ZipEntry(mFile.getName());
            try {
                mInputStream = new FileInputStream(mFile);
                mZipOutputStream = new ZipOutputStream(new FileOutputStream(new File(zipfile)));
                mZipOutputStream.putNextEntry(mEntry);
                mZipOutputStream.setComment("by raymond");
                int len = 0;
                byte[] buffer = new byte[1024 * 4];
                while ((len = mInputStream.read(buffer)) != -1) {
                    mZipOutputStream.write(buffer, 0, len);
                }
                mZipOutputStream.closeEntry();
                if (mInputStream != null) {
                    mInputStream.close();
                }
                return true;
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            try {
                mZipOutputStream = new ZipOutputStream(new FileOutputStream(new File(zipfile)));
                File[] mFiles = mFile.listFiles();
                for (int i = 0; i < mFile.length(); i++) {
                    mInputStream = new FileInputStream(mFiles[i]);
                    ZipEntry mEntry = new ZipEntry(mFile.getName() + File.separator + mFiles[i].getName());
                    mZipOutputStream.putNextEntry(mEntry);
                    int len = 0;
                    byte[] buffer = new byte[1024 * 4];
                    while ((len = mInputStream.read(buffer)) != -1) {
                        mZipOutputStream.write(buffer, 0, len);
                    }
                }
                mZipOutputStream.closeEntry();
                if (mInputStream != null) {
                    mInputStream.close();
                }
                return true;
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    public static void unzipFolder(String zipfile, String desfile) {
        File mFile = new File(zipfile);
        if (!mFile.exists()) {
            return;
        }
        ZipFile mZipFile = null;
        ZipInputStream mZipInputStream = null;
        try {
            mZipFile = new ZipFile(mFile);
            mZipInputStream = new ZipInputStream(new FileInputStream(mFile));
            ZipEntry mEntry = null;
            BufferedInputStream mInputStream = null;
            BufferedOutputStream mOutputStream = null;
            String name = "";
            while ((mEntry = mZipInputStream.getNextEntry()) != null) {
                name = mEntry.getName();
                if (mEntry.isDirectory()) {
                    File mUnzipfile = new File(desfile);
                    if (!mUnzipfile.getParentFile().exists()) {
                        mUnzipfile.mkdirs();
                    }
                } else {
                    File mEntryFile = new File(desfile);
                    if (!mEntryFile.getParentFile().exists()) {
                        mEntryFile.mkdirs();
                    }
                    if (!mEntryFile.exists()) {
                        mEntryFile.createNewFile();
                    }
                    mInputStream = new BufferedInputStream(mZipFile.getInputStream(mEntry));
                    mOutputStream = new BufferedOutputStream(new FileOutputStream(mEntryFile));
                    int len = 0;
                    byte[] buffer = new byte[1024 * 4];
                    while ((len = mInputStream.read(buffer)) != -1) {
                        mOutputStream.write(buffer, 0, len);
                    }
                }
            }
            if (mInputStream != null) {
                mInputStream.close();
            }
            if (mOutputStream != null) {
                mOutputStream.close();
            }
            if (mZipInputStream != null) {
                mZipInputStream.close();
            }
        } catch (ZipException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
